package com.abel.bigwater.wflow.mapper

import org.junit.Assert
import com.abel.bigmeter.service.DataParam
import com.abel.bigmeter.service.ZoneParam
import com.alibaba.fastjson.JSON
import org.junit.Assert.assertTrue
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import kotlin.test.assertNotNull

@SpringBootTest
@RunWith(SpringRunner::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class DataMapperTest {
    @Autowired
    var dataMapper: DataMapper? = null

    @Autowired
    var meterMapper: MeterMapper? = null

    @Autowired
    var zoneMapper: ZoneMapper? = null

    /**
     * 列举一个水司的全部水表的最后一条行度。
     */
    @Test
    fun test11MeterStatus() {
        val dlist = dataMapper!!.scadaMeterList(DataParam(firmId = "23"))
        lgr.info("scada list for firm 23: ${dlist.size}")

        assertTrue("should scada more than 1", dlist.isNotEmpty())
        lgr.info(JSON.toJSONString(dlist, true))
    }

    /**
     * 列举水司的全部水表的最后日水量.
     */
    @Test
    fun test12MeterDayUsage() {
        val dlist = zoneMapper!!.listMeterDayFlowYest(ZoneParam(firmId = "23"))
        lgr.info("scada list for firm 23: ${dlist?.size}")

        assertTrue("should scada more than 1", dlist?.isNotEmpty() == true)
        lgr.info(JSON.toJSONString(dlist, true))
    }

    /**
     * 获取最后三天的数据列表
     */
    @Test
    fun test21Data3Days() {
        val drange = dataMapper?.realtimeDateRange(DataParam(meterId = TEST_METER_ID))
        if (drange?.maxDateTime != null) {
            val dlist = dataMapper?.selectRealtime(DataParam(meterId = TEST_METER_ID).apply {
                sampleTime1 = drange.maxDateTime?.minusDays(3)?.toDate()
                sampleTime2 = drange.maxTime
            })

            assertTrue(dlist?.size ?: 0 > 1)
            lgr.info("data count: ${dlist?.size}: \n${JSON.toJSONString(dlist?.take(3), true)}\n......")
        }
    }

    /**
     * 获取最后30日的日水量
     */
    @Test
    fun test22Usage30Days() {
        val drange = zoneMapper?.meterDayFlowRange(ZoneParam(firmId = "%",
                meterId = TEST_DAY_METER))
        assertNotNull(drange?.firstOrNull())

        if (drange?.firstOrNull() != null) {
            val endDate = drange.first().maxDateTime
            val startDate = endDate?.minusDays(30)

            val ulist = zoneMapper?.listMeterDayFlow(ZoneParam(firmId = "%",
                    meterId = TEST_DAY_METER).apply {
                time1 = endDate?.minusDays(30)?.toDate()
                time2 = endDate?.plusDays(1)?.toDate()
            })

            assertTrue(ulist?.size ?: 0 >= 30)
            lgr.info("days count: ${ulist?.size}\n${JSON.toJSONString(ulist?.take(3), true)}\n......")
        }
    }

    companion object {
        const val TEST_METER_ID = "006001"
        const val TEST_DAY_METER = "km-19068-01"

        private val lgr = LoggerFactory.getLogger(DataMapperTest::class.java)
    }
}